classdef Solver < handle
    properties
        K       % 约化后的全局刚度矩阵
        F       % 约化后的荷载向量
        ID      % 全局自由度映射矩阵 (nNode x nDOFPerNode)
        Ured    % 求解得到的自由节点位移
        Ufull   % 还原后的完整位移向量（含约束节点）
    end

    methods
        function obj = Solver(K, F, ID)
            obj.K = K;
            obj.F = F;
            obj.ID = ID;
        end

        function solve(obj)
            % 直接求解 Ku = F
            obj.Ured = obj.K \ obj.F;

            % 还原到完整系统
            nNode = size(obj.ID, 1);
            nDOFPerNode = size(obj.ID, 2);
            totalDOF = nNode * nDOFPerNode;
            obj.Ufull = zeros(totalDOF, 1);

            for i = 1:nNode
                for j = 1:nDOFPerNode
                    dofID = obj.ID(i, j);
                    if dofID > 0
                        obj.Ufull((i-1)*nDOFPerNode + j) = obj.Ured(dofID);
                    else
                        obj.Ufull((i-1)*nDOFPerNode + j) = 0; % 约束自由度置零
                    end
                end
            end
        end

        function printResults(obj)
            nNode = size(obj.ID, 1);
            nDOFPerNode = size(obj.ID, 2);

            fprintf('\n=== 节点位移结果 ===\n');
            for i = 1:nNode
                idx = (i-1)*nDOFPerNode + (1:nDOFPerNode);
                fprintf('Node %3d: ', i);
                fprintf('%12.4e ', obj.Ufull(idx));
                fprintf('\n');
            end
        end
    end
end
